#include "modelTable.h"
#include "modelPointTable.h"
#include "modelCommandTable.h"
#include "db.h"
#include "logger.h"
#include "version.h"
#include <string.h>
#include <stdlib.h>

#define MODEL_TABLE_NAME "t_model"
#define SQL_MODEL_TABLE_CREATE "CREATE TABLE IF NOT EXISTS " MODEL_TABLE_NAME " (id INT PRIMARY KEY NOT NULL, name TEXT NOT NULL, desc CHAR(256))"
#define SQL_MODEL_TABLE_INSERT(...) "INSERT INTO " MODEL_TABLE_NAME "(id, name, desc) VALUES (" #__VA_ARGS__ ")"
#define SQL_MODEL_TABLE_REPLACE(...) "REPLACE INTO " MODEL_TABLE_NAME "(id, name, desc) VALUES (" #__VA_ARGS__ ")"
#define SQL_MODEL_TABLE_DELETE(id) "DELETE FROM " MODEL_TABLE_NAME " WHERE id = " #id
#define SQL_MODEL_TABLE_SELECT(id) "SELECT * FROM " MODEL_TABLE_NAME " WHERE id = " #id


/*===============================================================*/
/* 模型 */
int createModelTable(){
    return db_create_table(SQL_MODEL_TABLE_CREATE);
}

int insertModel(T_MODEL* model){
    if(NULL == model){
        log_file_e(APPNAME, "insert model is NULL");
        return -1;
    }
    if((NULL == model->name) || (1 > strlen(model->name))){
        log_file_e(APPNAME, "insert model name is NULL or empty");
        return -1;
    }
    return db_insert(SQL_MODEL_TABLE_INSERT(model->id, model->name, model->desc));
}

int replaceModel(T_MODEL* model){
    if(NULL == model){
        log_file_e(APPNAME, "replace model is NULL");
        return -1;
    }
    if((NULL == model->name) || (1 > strlen(model->name))){
        log_file_e(APPNAME, "replace model name is NULL or empty");
        return -1;
    }
    return db_insert(SQL_MODEL_TABLE_REPLACE(model->id, model->name, model->desc));
}

int deleteModel(T_MODEL* model){
    if(NULL == model){
        log_file_e(APPNAME, "delete model is NULL");
        return -1;
    }
    return db_delete(SQL_MODEL_TABLE_DELETE(model->id));
}

static int modelCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "modelCallback data is NULL");
        return -1;
    }
    T_MODEL_LIST* modelList = (T_MODEL_LIST*)data;
    if((0 == modelList->count) || (NULL == modelList->head)){
        modelList->head = modelList->tail = (T_MODEL*)malloc(sizeof(T_MODEL));
        modelList->head->next = NULL;
        modelList->head->prev = NULL;
    } else {
        modelList->tail->next = (T_MODEL*)malloc(sizeof(T_MODEL));
        modelList->tail->next->prev = modelList->tail;
    }
    modelList->count++;
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            modelList->tail->id = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "name")){
            if(NULL == argv[i]){
                modelList->tail->name[0] = '\0';
            } else {
                strcpy(modelList->tail->name, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "desc")){
            if(NULL == argv[i]){
                modelList->tail->desc[0] = '\0';
            } else {
                strcpy(modelList->tail->desc, argv[i]);
            }
        }
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return modelList->count;
}

int selectModel(const char* sql, T_MODEL_LIST* modelList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == modelList)){
        log_file_e(APPNAME, "select sql is NULL");
        return -1;
    }
    return db_select(sql, modelCallback, modelList);

}
//
void dbModelInit(){
    createModelTable();
    createModelPointTable();
    createModelCommandPointTable();
}
